package fragment;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
//import android.icu.util.Calendar;
import android.os.Bundle;
import java.util.Calendar;
import java.util.Date;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;

import com.example.shuaijiguang.android_tiku_demo.R;

import tools.DateAndTimeHepler;
import tools.LogHelper;



 //类别列表界面

public class SearchFragment extends BaseFragment {
	private static final String TAG = "SearchFragment";

	
	private Button mStartDate;
	private Button mEndDate;

	final int DATE_DIALOG = 1;


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		LogHelper.d(TAG, "onCreateView()-------------");
		
		View root = inflater.inflate(R.layout.fragment_search, container, false);

		initView(root);
//		initSwipe(root);
//		initGridView(root);
//
//		mSwipeRefreshLayout.setRefreshing(true);
//		loadRemoteData();
		
		return root;
	}

	private void initView(View root){
		mStartDate = (Button)root.findViewById(R.id.start_date_btn);

		mStartDate.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
//				showDialog(DATE_DIALOG);
				showDatePicker(startdate);
			}
		});

		mEndDate = (Button)root.findViewById(R.id.end_date_btn);
		mEndDate.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				showDatePicker(enddate);
			}
		});

		Date now = new Date();
		String nowString = DateAndTimeHepler.format(now);
		mStartDate.setText(nowString);
		mEndDate.setText(nowString);

	}

	@Override 
    public void setUserVisibleHint(boolean isVisibleToUser) {
    	super.setUserVisibleHint(isVisibleToUser);
    	
    	if(getUserVisibleHint()){

    	}else{
    		
    	}
    }

	private void showDatePicker(OnDateSetListener ondate) {
		DatePickerFragment date = new DatePickerFragment();

		/**
		 * Set Up Current Date Into dialog
		 */
		Calendar calender = Calendar.getInstance();
		Bundle args = new Bundle();
		args.putInt("year", calender.get(Calendar.YEAR));
		args.putInt("month", calender.get(Calendar.MONTH));
		args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
		date.setArguments(args);
		/**
		 * Set Call back to capture selected date
		 */
		date.setCallBack(ondate);
		date.show(getFragmentManager(), "Date Picker");
	}

	DatePickerDialog.OnDateSetListener enddate = new OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
							  int dayOfMonth) {

			mEndDate.setText(String.valueOf(year) + "-" + String.valueOf(monthOfYear+1)
					+ "-" + String.valueOf(dayOfMonth));

//			edittext.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear+1)
//					+ "-" + String.valueOf(year));
		}
	};

	DatePickerDialog.OnDateSetListener startdate = new OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
							  int dayOfMonth) {

			mStartDate.setText(String.valueOf(year) + "-" + String.valueOf(monthOfYear+1)
					+ "-" + String.valueOf(dayOfMonth));

//			edittext.setText(String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear+1)
//					+ "-" + String.valueOf(year));
		}
	};

//	@Override
//	protected Dialog onCreateDialog(int id) {
//		switch (id) {
//			case DATE_DIALOG:
//				return new DatePickerDialog(this, mdateListener, mYear, mMonth, mDay);
//		}
//		return null;
//	}
	
//	private void initSwipe(View root){
//		mSwipeRefreshLayout = (SwipeRefreshLayout)root.findViewById(R.id.swipe_refresh);
//		mSwipeRefreshLayout.setColorSchemeResources(R.color.color1, R.color.color2,
//				R.color.color3, R.color.color4);
//
//		mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//			@Override
//			public void onRefresh() {
//				LogHelper.d(TAG, "开始刷新~~~");
//
//				loadRemoteData();
//			}
//		});
//	}
//
//	private void initGridView(View root){
//		mGridView = (GridView)root.findViewById(R.id.grid_view);
//
//		adapter = new QuickAdapter<Catelog>(this.getActivity(), R.layout.gridview_item) {
//			@Override
//			protected void convert(BaseAdapterHelper helper, Catelog item) {
//				helper.setText(R.id.tv_cate_title, item.name);
//				helper.setImageUrl(R.id.iv_cate_icon,
//						AsyncHttpHelper.URL_IMG_BASE + "/" + item.icon);
//			}
//		};
//		//adapter = new MyAdapter();
//		mGridView.setAdapter(adapter);
//
//		mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				Intent intent = new Intent(SearchFragment.this.ctx, QuestionListActivity.class);
//				intent.putExtra("cate_id", adapter.getItem(position).id);
//				intent.putExtra("cate_name", adapter.getItem(position).name);
//
//				startActivity(intent);
//			}
//		});
//	}
//
//	private void loadRemoteData(){
//		AsyncHttpHelper.get(AsyncHttpHelper.URL_CATALOG_LIST,
//				new AsyncHttpHelper.MyResponseHandler(this.getActivity()) {
//			@Override
//			public void success(String content) {
//				ArrayList<Catelog> list = GsonHelper.getGson().fromJson(content,
//						new TypeToken<ArrayList<Catelog>>(){}.getType());
//
//				mSwipeRefreshLayout.setRefreshing(false);
//
//				adapter.addAll(list);
//				adapter.notifyDataSetChanged();
//			}
//		});
//	}
	

}
